package com.app.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;

import com.app.R;
import com.app.adapter.RecordAdapter;
import com.app.constant.Constants;
import com.app.http.HttpDataApi;
import com.app.http.HttpDataLoader;
import com.app.http.HttpDataReqUrl;
import com.app.manager.BetterLinearLayoutManager;
import com.app.model.bean.Page;
import com.app.model.bean.Record;
import com.app.model.request.GetOrderReq;
import com.app.model.request.ReqObj;
import com.app.model.response.ResObj;
import com.app.utils.ToastUtil;
import com.app.view.pulltorefresh.PullToRefreshLayout;
import com.app.view.pulltorefresh.PullableRecyclerView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class ActivityRecords extends ActivityBaseMenuText implements PullToRefreshLayout.OnRefreshListener, HttpDataApi.OnRequestCallback {

    private static final String LOG_TAG = ActivityRecords.class.getSimpleName();
    @BindView(R.id.recyclerView)
    PullableRecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    PullToRefreshLayout refreshLayout;
//    @BindView(R.id.llResult)
//    LinearLayout llResult;

    private boolean isVisibleToUser = false;

    private HttpDataLoader dataLoader;

    private RecordAdapter adapter;

    private Page page;

    private int status;

    private BroadcastReceiver receiver;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.e(LOG_TAG, "onCreate");
        super.onCreate(savedInstanceState);
        dataLoader = new HttpDataLoader(this);
        setContentView(R.layout.activity_records);
        setTitle("使用记录");
        ButterKnife.bind(this);
        refreshLayout.setOnRefreshListener(this);
        recyclerView.setLayoutManager(new BetterLinearLayoutManager(this));
        adapter = new RecordAdapter(this);
        recyclerView.setAdapter(adapter);
        receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent != null && intent.getAction().equals(Constants.ACTION_REFRESH_ORDERS)) {
                    page = null;
                    getRecords();
                }
            }
        };
        IntentFilter filter = new IntentFilter();
        filter.addAction(Constants.ACTION_REFRESH_ORDERS);
        registerReceiver(receiver, filter);
        getRecords();
    }


    private void getRecords() {
        ReqObj req = new ReqObj();
        if (page == null) {
            page = new Page();
        } else {
            if (!page.nextPage()) {
                // 加载更多失败
                refreshLayout.loadmoreFinish(PullToRefreshLayout.FAIL);
                return;
            }
        }
        req.setPage(page);
        dataLoader.POST(HttpDataReqUrl.URL_GET_RECORDS, req);
        showLoadingDialog();
    }

    @Override
    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
        page = null;
        getRecords();
    }

    @Override
    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
        getRecords();
    }


    @Override
    public void onRequestSuccess(String url, ResObj result, boolean isFrmCache) {
        Log.e(LOG_TAG, "onRequestSuccess");
        Log.e(LOG_TAG, "url: " + url);
        Log.e(LOG_TAG, "result: " + result);
        dissmissLoadingDialog();
        if (url.equals(HttpDataReqUrl.URL_GET_RECORDS)) {
            if (result.getCode() == Constants.CODE_SUCCESS) {
                if (result.getData() instanceof List) {
                    List<Record> items = (List<Record>) result.getData();
                    if (result.getPage() != null) {
                        page = result.getPage();
                    }
                    if (page.getPageNum() == 1) {
                        adapter.setItems(items);
                        refreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                    } else {
                        adapter.addItems(items);
                        refreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
                    }
                } else {
                    if (page.getPageNum() == 1) {
                        refreshLayout.refreshFinish(PullToRefreshLayout.FAIL);
                    } else {
                        refreshLayout.loadmoreFinish(PullToRefreshLayout.FAIL);
                    }
                    ToastUtil.getInstance().showToast(ActivityRecords.this, result.getMsg());
                }

            } else {
                ToastUtil.getInstance().showToast(ActivityRecords.this, result.getMsg());
                refreshLayout.refreshFinish(PullToRefreshLayout.FAIL);
            }
        }


    }

    @Override
    public void onRequestError(String url, int statusCode, String error) {
        Log.e(LOG_TAG, "onRequestError");
        Log.e(LOG_TAG, "url: " + url);
        Log.e(LOG_TAG, "statusCode: " + statusCode);
        Log.e(LOG_TAG, "error: " + error);
        dissmissLoadingDialog();
        if (isVisibleToUser) {
            ToastUtil.getInstance().showToast(ActivityRecords.this, error);
            if (page != null && page.getPageNum() > 1) {
                refreshLayout.loadmoreFinish(PullToRefreshLayout.FAIL);
            } else {
                refreshLayout.refreshFinish(PullToRefreshLayout.FAIL);
            }
        }
    }


    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
